package com.Client;

import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;





import com.Serverce.Config;
import com.Util.GeneralThreadPool;

public class CheckThread extends TimerTask {
	
	private ScheduledFuture<?> _timer;
	
	public void start() {
		final int timeMillis = 3000;// 1秒
		this._timer = GeneralThreadPool.getInstance().scheduleAtFixedRate(this,timeMillis, timeMillis);
	}
	
	@Override
	public void run() {
		try{
		Iterator<ClientThread> ite =Config.CLIENT_THREAD.iterator();
		 final long nowTime = System.currentTimeMillis();
		while(ite.hasNext()){
			ClientThread ct = ite.next();
//			System.out.println(ct.getIP()+" "+ct._isOK);
			if(ct._isOK == false && nowTime - ct.loginTime >= 5000){
				ct.close();
				System.out.println(ct.getIP()+" 閒置過久，結束連線。");
			}
		}
		
		}catch(Exception e){
			e.printStackTrace();
			GeneralThreadPool.getInstance().cancel(this._timer, false);
			final CheckThread Timer = new CheckThread();
			Timer.start();
		}
	}

}
